//
//  SmallDecisionsWebViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/13.
//

#import "SmallDecisionsWebViewController.h"
#import <WebKit/WebKit.h>

@interface SmallDecisionsWebViewController ()
@property (strong, nonatomic) WKWebView *smallDecisionsWebView;
@property (strong, nonatomic) UIView    *smallDecisionsPropressView;
@end

@implementation SmallDecisionsWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self createSmallDecisionsWebViewUI];
}

- (void)createSmallDecisionsWebViewUI {
    NSURLRequest *request = [NSURLRequest requestWithURL:self.webUrl];
    [self.smallDecisionsWebView loadRequest:request];
    [self.smallDecisionsWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [self.smallDecisionsWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    
    UIView *smallDecisionsPropressView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 2)];
    smallDecisionsPropressView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:smallDecisionsPropressView];
    self.smallDecisionsPropressView = smallDecisionsPropressView;
}

- (WKWebView *)smallDecisionsWebView {
    if (!_smallDecisionsWebView) {
        _smallDecisionsWebView = [[WKWebView alloc] initWithFrame: CGRectZero];
        _smallDecisionsWebView.backgroundColor = [UIColor clearColor];
        [self.view addSubview: _smallDecisionsWebView];
        [_smallDecisionsWebView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(0);
        }];
    }
    return _smallDecisionsWebView;
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSString *,id> *)change
                       context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.smallDecisionsPropressView.frame = CGRectMake(0, 0, self.view.bounds.size.width * [change[NSKeyValueChangeNewKey] floatValue], 2);
        if ([change[NSKeyValueChangeNewKey] floatValue] == 1) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.smallDecisionsPropressView.alpha = 0;
            });
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.smallDecisionsPropressView.frame = CGRectMake(0, 0, 0, 3);
            });
        }
    } else if ([keyPath isEqualToString:@"title"]) {
        self.title = change[NSKeyValueChangeNewKey];
    }
}
- (BOOL)showNavBarCloseButton {
    return YES;
}

- (void)closeBarButtonAction:(UIView *)view {
    [SmallDecisionsNavigatorManager dismissViewControllerAnimated:YES completion:nil];
}

- (void)dealloc {
    [self.smallDecisionsWebView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.smallDecisionsWebView removeObserver:self forKeyPath:@"title"];
}

@end
